﻿using DeviceMonitor.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceMonitor.ViewModel
{
    class LoadViewModel : RecordViewModel
    {
        public ObservableCollection<Billing> _billingList = new ObservableCollection<Billing>();
        public ObservableCollection<LoadRecord> _loadRecordList = new ObservableCollection<LoadRecord>();

        public ObservableCollection<Billing> billingList
        {
            get { return _billingList; }
            set
            {
                _billingList = value;
                RaisePropertyChanged("billingList");
            }
        }
        public ObservableCollection<LoadRecord> loadRecordList
        {
            get { return _loadRecordList; }
            set
            {
                _loadRecordList = value;
                RaisePropertyChanged("loadRecordList");
            }
        }

        public void LoadData(string jsonStr)
        {
            try
            {
                JObject jsonObj = JObject.Parse(jsonStr);
                bool success = (bool)jsonObj.GetValue("success");
                if (!success) return;
                billingList.Clear();

                List<Billing> data = JsonConvert.DeserializeObject<List<Billing>>(jsonObj["data"].ToString());

                foreach (Billing item in data)
                {
                    billingList.Add(item);
                }
            }
            catch (Exception ex) { }
        }

        public void LoadRecordData(string jsonStr)
        {
            try
            {
                JObject jsonObj = JObject.Parse(jsonStr);
                bool success = (bool)jsonObj.GetValue("success");
                if (!success) return;
                loadRecordList.Clear();

                List<LoadRecord> data = JsonConvert.DeserializeObject<List<LoadRecord>>(jsonObj["data"].ToString());

                foreach (LoadRecord item in data)
                {
                    loadRecordList.Add(item);
                }
            }
            catch (Exception ex) { }
        }

        public QueryParamModel _paramModel = new QueryParamModel();
        public QueryParamModel queryParam
        {
            get { return _paramModel; }
            set
            {
                _paramModel = value;
                RaisePropertyChanged("queryParam");
            }
        }
    }
}
